package cn.lyq.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ConnectDB {
	public static Connection conn;

	public Connection getConnection() {
		if (conn == null) {
			try {
				Class.forName("com.mysql.cj.jdbc.Driver");
				conn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/ECDICT?severTimezone=GMT","root","root");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return conn;
	}

	public boolean addWord(String english, String chinese) {
		conn = getConnection();
		boolean ok = false;
		try {
			PreparedStatement pstmt = conn.prepareStatement("INSERT INTO ecdict (english,chinese) VALUES(?,?)");
			pstmt.setString(1, english);
			pstmt.setString(2, chinese);
			int result = pstmt.executeUpdate();
			if(result==1) ok=true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ok;

	}

	// 用于测试
	public static void main(String[] args)  {
		new ConnectDB().addWord("test", "测试");
	}

}